﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using Speakup;
using System.Windows;
using System.Windows.Media;
using System.Windows.Documents;
using System.Globalization;

namespace SpeakupUI
{
    class ClassInfoPanel: ContentControl 
    {
        ClassPanelList ClassPanelList;
        DockPanel Panel;

        public ClassInfoPanel(ClassPanelList classes)
        {
            ClassPanelList = classes;
            ClassPanelList.HighlightEvent += HighlightHandler;
            ClassPanelList.SelectEvent += HighlightHandler;

            Panel = new DockPanel();
            Panel.Height = 200;            

            Border border = new Border();
            border = new Border();
            border.BorderThickness = new Thickness(2);
            border.BorderBrush = Brushes.Black;

            border.Child = Panel;

            Content = border;

            UpdateInfo(null);
        }

        void UpdateInfo(Class speakupClass)
        {
            Panel.Children.Clear();

            if (speakupClass == null)
            {
                Panel.Background = Brushes.LightYellow;

                TextBlock box = new TextBlock(new Bold(new Run("Select or hover over any class "+
                                                                "for more details about it.")));
                box.HorizontalAlignment = HorizontalAlignment.Center;
                box.VerticalAlignment = VerticalAlignment.Center;
                box.TextAlignment = TextAlignment.Center;
                box.TextWrapping = TextWrapping.Wrap;
                box.Padding = new Thickness(10);
                Panel.Children.Add(box);
                return;
            }

            UpdateStatus(speakupClass);            

            Panel.Background = ClassPanel.ClassBackgrounds[speakupClass.ClassStatus];

            TextBlock title = new TextBlock();
            title.HorizontalAlignment = HorizontalAlignment.Center;
            title.VerticalAlignment = VerticalAlignment.Center;
            title.TextAlignment = TextAlignment.Center;

            //name
            {
                Run className = new Run(speakupClass.ClassName.ToString());
                title.Inlines.Add(new Bold(className));
            }

            title.Inlines.Add(new Run("\n"));

            //time
            {
                TimeSlot timeSlot = new TimeSlot(speakupClass.ClassTime.Begin, speakupClass.ClassTime.End);
                Run classTime = new Run(timeSlot.ToString());
                title.Inlines.Add(classTime);
            }

            title.Inlines.Add(new Run("\n"));

            //date
            {
                DateTime date = speakupClass.ClassTime.Date;
                string strDate = date.ToString("dddd\nMMMM dd", CultureInfo.InvariantCulture);
                Run classDate = new Run(strDate);
                title.Inlines.Add(classDate);
            }

            title.Inlines.Add(new Run("\n"));
            //date hint
            {
                DateTime classDate = speakupClass.ClassTime.Date;
                DateTime now = DateTime.Now.Date;
                
                TimeSpan delta = classDate - now;

                string weekHint = null;
                if (delta.TotalDays <= 7 && 
                    classDate.DayOfWeek >= now.DayOfWeek)
                {
                    weekHint = "this week";
                }
                else if (delta.TotalDays <= 7)
                {
                    weekHint = "next week";
                }
                else if (delta.TotalDays <= 7 &&
                    classDate.DayOfWeek > now.DayOfWeek)
                {
                    weekHint = "next week";
                }

                int days = (int)delta.TotalDays;
                
                string dayHint = days.ToString()+" day"+((days>1)?"s":"")+" left";
                string hint = dayHint;

                if(days == 0)
                {
                    hint = "today";
                }
                else if(days == 1)
                {
                    hint = "tomorrow";
                }
                else if (weekHint != null)
                {
                    hint = weekHint + ", " + hint;
                }

                hint = "(" + hint + ")";

                Italic italic = new Italic(new Run(hint));
                italic.FontSize = 11 ;
                title.Inlines.Add(italic);
            }


            Panel.Children.Add(title);
        }

        void UpdateStatus(Class speakupClass)
        {

            string message = null;
            string action = "";
            Brush background = null;
            ClassStatus desiredStatus = ClassStatus.Unknown;


            switch (speakupClass.ClassStatus)
            {
                case ClassStatus.Available:
                   message = "Available.\nYou can book this class.";
                   action = "Book class.";
                   background = Brushes.Yellow;
                   desiredStatus = ClassStatus.Reserved;                   
                   break;

                case ClassStatus.Reserved:
                    message = "Reserved by you.\nYou can cancel reservation.";
                    action = "Cancel reservation.";
                    background = Brushes.Pink;
                    desiredStatus = ClassStatus.Available;
                    break;

                case ClassStatus.AvailableWaitList:
                    message = "Availble for Wait List only.\nYou can sign up for wait list.";
                    action = "Signup for wait list.";
                    background = Brushes.Orange;
                    desiredStatus = ClassStatus.ReservedWaitList;
                    break;


                case ClassStatus.ReservedWaitList:
                    message = "You are on wait list.\nYou can cancel reservation.";
                    action = "Leave wait list.";
                    background = Brushes.Pink;
                    desiredStatus = ClassStatus.AvailableWaitList;
                    break;
            }

            if (message != null)
            {
                TextBlock block = new TextBlock();
                block.FontSize = 12;
                block.HorizontalAlignment = HorizontalAlignment.Center;
                block.VerticalAlignment = VerticalAlignment.Center;
                block.TextAlignment = TextAlignment.Center;
                
                block.Inlines.Add(new Italic(new Run(message)));

                Border border = new Border();
                border.Background = background;
                border.BorderThickness = new Thickness(0, 0, 0, 1);
                border.BorderBrush = Brushes.Black;
                border.Child = block;

                DockPanel.SetDock(border, Dock.Top);
                Panel.Children.Add(border);

                Button actionButton = new Button();
                actionButton.Content = action;
                actionButton.Background = background;
                actionButton.IsDefault = true;
                actionButton.Click += delegate
                {
                    if (speakupClass.Modified)
                    {
                        speakupClass.CancelModification();
                    }
                    else
                    {
                        speakupClass.DesiredStatus = desiredStatus;
                    }

                    UpdateInfo(speakupClass);
                };

                DockPanel.SetDock(actionButton, Dock.Bottom);
                Panel.Children.Add(actionButton);
            }

        }

        void HighlightHandler(object sender, EventArgs e)
        {
            ClassPanel classPanel = (ClassPanel)sender;
            if (classPanel.Highlight == true)
            {
                UpdateInfo(classPanel.SpeakupClass);
            }
            else
            {
                if (ClassPanelList.SelectedPanel != null)
                {
                    UpdateInfo(ClassPanelList.SelectedPanel.SpeakupClass);
                }
                else
                {
                    UpdateInfo(null);
                }
            }
        }      
    }
}
